package mia.beans;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.hibernate.Session;

import mia.dao.AdvertisementTypeEntityDAO;
import mia.dao.CategoryEntityDAO;
import mia.entity.AdvertisementEntity;
import mia.entity.AdvertisementTypeEntity;
import mia.entity.AttachmentEntity;
import mia.entity.CategoryEntity;
import mia.entity.UserEntity;
import mia.util.HibernateManager;

@ManagedBean(name="advertisementRegBean")
@SessionScoped
public class AdvertisementRegBean {

	@ManagedProperty("#{userBean.user}")
	private UserEntity user;	
	private AdvertisementEntity advertisement = new AdvertisementEntity();
	private List<AdvertisementTypeEntity> types = null;
	private AdvertisementTypeEntity selectedType;
	private List<CategoryEntity> categories = null;
	private CategoryEntity selectedCategory;
	private String attatchment1;
	private String attatchment2;
	private String attatchment3;

	public AdvertisementEntity getAdvertisement() {
		return advertisement;
	}
	public void setAdvertisement(AdvertisementEntity advertisement) {
		this.advertisement = advertisement;
	}
	public List<AdvertisementTypeEntity> getTypes() {
		if(types == null)
			types = AdvertisementTypeEntityDAO.getAdvertisementTypes();
		return types;
	}
	public void setTypes(List<AdvertisementTypeEntity> types) {
		this.types = types;
	}
	public List<CategoryEntity> getCategories() {
		if(categories == null)
			categories = CategoryEntityDAO.getCategories();
		return categories;
	}
	public void setCategories(List<CategoryEntity> categories) {
		this.categories = categories;
	}
	public AdvertisementTypeEntity getSelectedType() {
		return selectedType;
	}
	public void setSelectedType(AdvertisementTypeEntity selectedType) {
		this.selectedType = selectedType;
	}
	public CategoryEntity getSelectedCategory() {
		return selectedCategory;
	}
	public void setSelectedCategory(CategoryEntity selectedCategory) {
		this.selectedCategory = selectedCategory;
	}
	public String getAttatchment1() {
		return attatchment1;
	}
	public void setAttatchment1(String attatchment1) {
		this.attatchment1 = attatchment1;
	}
	public String getAttatchment2() {
		return attatchment2;
	}
	public void setAttatchment2(String attatchment2) {
		this.attatchment2 = attatchment2;
	}
	public String getAttatchment3() {
		return attatchment3;
	}
	public void setAttatchment3(String attatchment3) {
		this.attatchment3 = attatchment3;
	}
	public UserEntity getUser() {
		return user;
	}
	public void setUser(UserEntity user) {
		this.user = user;
	}
	
	public String trySave()
	{
		AttachmentEntity att1 = new AttachmentEntity();
		att1.setPath(attatchment1);
		att1.setAdvertisement(advertisement);
		
		AttachmentEntity att2 = new AttachmentEntity();
		att2.setPath(attatchment2);
		att2.setAdvertisement(advertisement);
		
		AttachmentEntity att3 = new AttachmentEntity();
		att3.setPath(attatchment3);
		att3.setAdvertisement(advertisement);
		
		advertisement.getAttatchments().add(att1);
		advertisement.getAttatchments().add(att2);
		advertisement.getAttatchments().add(att3);
		
		advertisement.setUser(user);
		
		advertisement.setCategory(selectedCategory);
		
		advertisement.setType(selectedType);
		
		Session session = HibernateManager.getSession();
		session.beginTransaction();
		session.save(advertisement);
		session.getTransaction().commit();
		session.close();
		return "result";
	}
}
